﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public static class ExtensionsDateTime
    {
        public static DateTime me_StartOfWeek(this DateTime dateTime, DayOfWeek startOfWeek)
        {
            int diff = dateTime.DayOfWeek - startOfWeek;
            if (diff < 0)
            {
                diff += 7;
            }

            return dateTime.AddDays(-1 * diff);
        }

        public static DateTime me_EndOfWeek(this DateTime dateTime, DayOfWeek endOfWeek)
        {
            int diff = endOfWeek - dateTime.DayOfWeek;
            if (diff < 0)
            {
                diff += 7;
            }

            return dateTime.AddDays(diff);
        }

        public static bool me_IsValid(this TimeSpan dateTime)
        {
            if (dateTime == TimeSpan.Zero || dateTime == TimeSpan.MinValue || dateTime == TimeSpan.MaxValue)
            {
                return false;
            }
            return true;
        }

        public static bool me_IsValid(this DateTime dateTime)
        {
            if (dateTime != DateTime.MinValue)
            {
                return true;
            }
            return false;
        }

        public static bool me_IsValid(this DateTime? dateTime)
        {
            if (dateTime.HasValue && dateTime.Value.me_IsValid())
            {
                return true;
            }
            return false;
        }

        public static DateTime me_GetValue(this DateTime? dateTime)
        {
            if (dateTime.HasValue)
            {
                return dateTime.Value;
            }
            return DateTime.MinValue;
        }

        public static bool me_IsValid(this TimeSpan? dateTime)
        {
            if (dateTime.HasValue && dateTime.Value.me_IsValid())
            {
                return true;
            }
            return false;
        }

        public static TimeSpan me_GetValue(this TimeSpan? dateTime)
        {
            if (dateTime.HasValue)
            {
                return dateTime.Value;
            }
            return TimeSpan.Zero;
        }

        public static DateTime GermanToEnglishDate(this string germanDate)
        {
            DateTime englishDate;
            String[] dateRipper = germanDate.Split('.');
            int day = int.Parse(dateRipper[0]);
            int month = int.Parse(dateRipper[1]);
            int year = int.Parse(dateRipper[2]);

            englishDate = new DateTime(year, month, day);
            return englishDate; 
        }
    }
}
